package com.lngsyz.eseal.base.biz.controller;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.lngsyz.eseal.base.biz.service.NotLoginService;
import com.lngsyz.eseal.base.entity.CommonConstant;
import com.lngsyz.eseal.base.entity.URIResult;
import com.lngsyz.eseal.base.entity.notLogin.CheckRandomCode;
import com.lngsyz.eseal.base.entity.notLogin.InFindPasswd;
import com.lngsyz.eseal.base.entity.notLogin.InGetRandomCode;
import com.lngsyz.eseal.base.entity.notLogin.InLogin;
import com.lngsyz.eseal.base.entity.notLogin.InRegist;
import com.lngsyz.eseal.base.entity.notLogin.OutGetRandomCode;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;

@Controller
@Api(tags = "NotLoginController", description = "不需要登录")
@RequestMapping("v1/notLogin")
public class NotLoginController {

	@Autowired()
	private NotLoginService notLoginService;

	@ApiOperation(value = "获取动态随机码图片", notes = "获取动态随机码图片")
	@RequestMapping(value = "/getRandomCode", method = { RequestMethod.GET })
	@ResponseBody
	public URIResult<OutGetRandomCode> getRandomCode(@ApiParam(name = "t", value = "业务类型（1：注册 2：找回密码）") @RequestParam(value = "t", defaultValue = "") String t,
			HttpServletRequest request) {
		URIResult<OutGetRandomCode> res = new URIResult<OutGetRandomCode>();
		try {
			InGetRandomCode inGetRandomCode = new InGetRandomCode();
			inGetRandomCode.setCodeType(t);
			OutGetRandomCode outGetRandomCode = notLoginService.getRandomCode(inGetRandomCode);
			res.setData(outGetRandomCode);
			res.setCode(CommonConstant.CODE_SUCCESS);
		} catch (Exception e) {
			res.setCode(CommonConstant.CODE_FAILURE);
			res.setInfo(e.getMessage());
		}
		return res;
	}
	
	@ApiOperation(value = "校验动态随机码，然后发送短信", notes = "校验动态随机码，然后发送短信")
	@ApiImplicitParam(name = "checkRandomCode", value = "校验动态随机码，然后发送短信", dataType = "CheckRandomCode", required = true, paramType = "body")
	@RequestMapping(value = "/checkRandomCode", method = RequestMethod.POST)
	@ResponseBody
	public URIResult<String> checkRandomCode(@RequestBody CheckRandomCode checkRandomCode) {
		URIResult<String> res = new URIResult<String>();
		try {
			notLoginService.checkRandomCode(checkRandomCode);
			res.setCode(CommonConstant.CODE_SUCCESS);
		} catch (Exception e) {
			res.setCode(CommonConstant.CODE_FAILURE);
			res.setInfo(e.getMessage());
		}
		return res;
	}
	
	@ApiOperation(value = "注册，同时返回token", notes = "注册，同时返回token")
	@ApiImplicitParam(name = "inRegist", value = "注册，同时返回token", dataType = "InRegist", required = true, paramType = "body")
	@RequestMapping(value = "/regist", method = RequestMethod.POST)
	@ResponseBody
	public URIResult<String> regist(@RequestBody InRegist inRegist) {
		URIResult<String> res = new URIResult<String>();
		try {
			String rtnStr = notLoginService.regist(inRegist);
			res.setData(rtnStr);
			res.setCode(CommonConstant.CODE_SUCCESS);
		} catch (Exception e) {
			res.setCode(CommonConstant.CODE_FAILURE);
			res.setInfo(e.getMessage());
		}
		return res;
	}
	
	@ApiOperation(value = "登录，同时返回token", notes = "登录，同时返回token")
	@ApiImplicitParam(name = "inLogin", value = "登录，同时返回token", dataType = "InLogin", required = true, paramType = "body")
	@RequestMapping(value = "/login", method = RequestMethod.POST)
	@ResponseBody
	public URIResult<String> login(@RequestBody InLogin inLogin) {
		URIResult<String> res = new URIResult<String>();
		try {
			String rtnStr = notLoginService.login(inLogin);
			res.setData(rtnStr);
			res.setCode(CommonConstant.CODE_SUCCESS);
		} catch (Exception e) {
			res.setCode(CommonConstant.CODE_FAILURE);
			res.setInfo(e.getMessage());
		}
		return res;
	}
	
	@ApiOperation(value = "找回密码", notes = "找回密码")
	@ApiImplicitParam(name = "inFindPasswd", value = "找回密码", dataType = "InFindPasswd", required = true, paramType = "body")
	@RequestMapping(value = "/findPasswd", method = RequestMethod.POST)
	@ResponseBody
	public URIResult<String> findPasswd(@RequestBody InFindPasswd inFindPasswd) {
		URIResult<String> res = new URIResult<String>();
		try {
			notLoginService.findPasswd(inFindPasswd);
			res.setCode(CommonConstant.CODE_SUCCESS);
		} catch (Exception e) {
			res.setCode(CommonConstant.CODE_FAILURE);
			res.setInfo(e.getMessage());
		}
		return res;
	}
}